package com.inspur.servlet.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.eclipse.jdt.internal.compiler.ast.ThisReference;

/**
 * @author  zhangteng 
 * @date 创建时间：2017年7月18日 
 */
public class DefaultConnection {

	// 驱动程序名
	//oracle.jdbc.driver.OracleDriver
	//com.mysql.jdbc.Driver
	private final String driver = "com.ibm.db2.jcc.DB2Driver";

	// URL指向要访问的数据库名scutcs
	//jdbc:oracle:thin:@localhost:1521:orcl
	//jdbc:mysql://localhost:3306/trustmore
	private final String url = "jdbc:db2://10.68.123.102:50000/HBADB";

	// MySQL配置时的用户名
	private final String user = "dddl";

	// Java连接MySQL配置时的密码
	private static final String password = "1";

	private static Connection conn = null;

	private DefaultConnection() {
	    try {
	        // 加载驱动程序
	        Class.forName(driver);
	        // 连续数据库
	        conn = DriverManager.getConnection(url, user, password);
	    } catch (ClassNotFoundException e) {
	        e.printStackTrace();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	}

	public static final Connection getConn() {
	    if (conn == null) {
	        synchronized (DefaultConnection.class) {
	            if (conn == null) {
	                new DefaultConnection();
	            }
	        }
	    }
	    try {
	        if (!conn.isClosed())
	            System.out.println("连接数据库成功!");
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return conn;
	}
	
	public static void close(){
		try {
			if(conn != null){
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		DefaultConnection.getConn();
	}
}

